package com.lz.bank.web;

import com.lz.bank.exceptions.MoneyNotEnoughException;
import com.lz.bank.exceptions.TransferException;
import com.lz.bank.service.AccountService;
import com.lz.bank.service.impl.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@WebServlet("/transfer")
public class AccountServlet extends HttpServlet {
    private AccountService accountService= new AccountServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取表单数据
        String fromActno = request.getParameter("fromActno");
        String toActno = request.getParameter("toActno");
        double money = Double.parseDouble(request.getParameter("money"));
        //System.out.println(fromActno + "\t" + toActno + "\t" + money);
        //调用service的转账方法进行转账
        try {
            accountService.transfer(fromActno,toActno,money);
        } catch (MoneyNotEnoughException e) {
            System.out.println( e.getMessage());
        } catch (TransferException e) {
            System.out.println( e.getMessage());
        }
        //调用视图层
        response.sendRedirect("/bank/success.html");
    }
}
